package com.chuangjiangx.member.common.utils;

import com.chuangjiangx.member.basic.ddd.application.command.AIMethod;
import com.chuangjiangx.member.basic.ddd.application.command.AIRequestCommand;
import com.chuangjiangx.member.common.utils.AIFace.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A接口工具
 */
public class AIFaceUtils {

    private static final Logger log = LoggerFactory.getLogger(AIFaceUtils.class);
    public static final String AI_HOST = "http://60.191.110.43:5600";
    /**
     * AI接口调用-方法
     * @param aiRequestCommand
     * @return
     * @throws AiException
     */
    public static AiResponse aiMethod(AIRequestCommand aiRequestCommand) throws AiException {

        log.info("AI人脸请求参数:{}", JacksonUtils.ObjectToJson(aiRequestCommand));

        AiResponse aiResponse = new AiResponse();
        IAiClient aiClient = new AiClient();

        switch(aiRequestCommand.getAiMethod()){
            case ADD_FACE:
                AIAddRequest aiAddRequest = new AIAddRequest();
                aiAddRequest.setImage(aiRequestCommand.getImage());
                aiAddRequest.setImage_type(aiRequestCommand.getAiFaceImageType().value);
                aiAddRequest.setUnion_id(aiRequestCommand.getUnion_id());
                aiResponse = aiClient.execute(aiAddRequest);
                if (AIConstant.CODE_SUCCESS.equals(aiResponse.getErrorCode())) {
                    AiAddFaceResponse aiAddFaceResponse = (AiAddFaceResponse) aiResponse.getData();
                    aiAddFaceResponse.setErrorCode(aiResponse.getErrorCode());
                    return aiAddFaceResponse;
                } else {
                    AiAddFaceResponse aiAddFaceResponse = new AiAddFaceResponse();
                    aiAddFaceResponse.setErrorCode(aiResponse.getErrorCode());
                    aiAddFaceResponse.setErrorMsg(aiResponse.getErrorMsg());
                    return aiAddFaceResponse;
                }
            case UPDATE_FACE:
                AIUpdateRequest aiUpdateRequest = new AIUpdateRequest();
                aiUpdateRequest.setImage(aiRequestCommand.getImage());
                aiUpdateRequest.setImage_type(aiRequestCommand.getAiFaceImageType().value);
                aiUpdateRequest.setUnion_id(aiRequestCommand.getUnion_id());
                aiUpdateRequest.setFace_id(aiRequestCommand.getFace_id());
                aiResponse = aiClient.execute(aiUpdateRequest);
                if (AIConstant.CODE_SUCCESS.equals(aiResponse.getErrorCode())) {
                    AIUpdateFaceResponse aiUpdateFaceResponse = (AIUpdateFaceResponse) aiResponse.getData();
                    aiUpdateFaceResponse.setErrorCode(aiResponse.getErrorCode());
                    return aiUpdateFaceResponse;
                } else {
                    AIUpdateFaceResponse aiUpdateFaceResponse = new AIUpdateFaceResponse();
                    aiUpdateFaceResponse.setErrorCode(aiResponse.getErrorCode());
                    aiUpdateFaceResponse.setErrorMsg(aiResponse.getErrorMsg());
                    return aiUpdateFaceResponse;
                }
            case RECOGNIZE_FACE:
                AIRecognizeFaceRequest  aiRecognizeFaceRequest = new AIRecognizeFaceRequest();
                aiRecognizeFaceRequest.setImage(aiRequestCommand.getImage());
                aiRecognizeFaceRequest.setImage_type(aiRequestCommand.getAiFaceImageType().value);
                aiRecognizeFaceRequest.setUnion_id(aiRequestCommand.getUnion_id());
                aiRecognizeFaceRequest.setFace_ids(aiRequestCommand.getFace_ids());
                aiResponse = aiClient.execute(aiRecognizeFaceRequest);
                if (AIConstant.CODE_SUCCESS.equals(aiResponse.getErrorCode())) {
                    AiRecognizeFaceResponse aiRecognizeFaceResponse = (AiRecognizeFaceResponse) aiResponse.getData();
                    aiRecognizeFaceResponse.setErrorCode(aiResponse.getErrorCode());
                    return aiRecognizeFaceResponse;
                } else {
                    AiRecognizeFaceResponse aiRecognizeFaceResponse = new AiRecognizeFaceResponse();
                    aiRecognizeFaceResponse.setErrorCode(aiResponse.getErrorCode());
                    aiRecognizeFaceResponse.setErrorMsg(aiResponse.getErrorMsg());
                    return aiRecognizeFaceResponse;
                }
            case BIOASSAY_FACE:
                AIBioassayFaceRequest  aiBioassayFaceRequest = new AIBioassayFaceRequest();
                aiBioassayFaceRequest.setVideo_type(aiRequestCommand.getAiFaceVideoType().value);
                aiBioassayFaceRequest.setVideo_url(aiRequestCommand.getVideo_url());
                aiResponse = aiClient.execute(aiBioassayFaceRequest);
                if (AIConstant.CODE_SUCCESS.equals(aiResponse.getErrorCode())) {
                    AiBioassayFaceResponse aiBioassayFaceResponse = (AiBioassayFaceResponse) aiResponse.getData();
                    aiBioassayFaceResponse.setErrorCode(aiResponse.getErrorCode());
                    return aiBioassayFaceResponse;
                } else {
                    AiBioassayFaceResponse aiBioassayFaceResponse = new AiBioassayFaceResponse();
                    aiBioassayFaceResponse.setErrorCode(aiResponse.getErrorCode());
                    aiBioassayFaceResponse.setErrorMsg(AiExceptionCode.get(aiResponse.getErrorCode()).msg);
                    return aiBioassayFaceResponse;
                }
            case INPUTFACE_PRECOLLECT:
                AIAddFacePerProcessRequest aiAddFacePerProcessRequest = new AIAddFacePerProcessRequest();
                aiAddFacePerProcessRequest.setMember_id(aiRequestCommand.getMember_id());
                aiAddFacePerProcessRequest.setUnion_id(aiRequestCommand.getUnion_id());
                aiResponse = aiClient.execute(aiAddFacePerProcessRequest);
                if (AIConstant.CODE_SUCCESS.equals(aiResponse.getErrorCode())) {
                    AIAddFacePerProcessResponse aiAddFacePerProcessResponse = (AIAddFacePerProcessResponse) aiResponse.getData();
                    aiAddFacePerProcessResponse.setErrorCode(aiResponse.getErrorCode());
                    return aiAddFacePerProcessResponse;
                }else {
                    AIAddFacePerProcessResponse aiAddFacePerProcessResponse = new AIAddFacePerProcessResponse();
                    aiAddFacePerProcessResponse.setErrorCode(aiResponse.getErrorCode());
                    aiAddFacePerProcessResponse.setErrorMsg(AiExceptionCode.get(aiResponse.getErrorCode()).msg);
                    return aiAddFacePerProcessResponse;
                }
            case INPUTFACE_COLLECT:
                AIFaceInputRequest aiFaceInputRequest = new AIFaceInputRequest();
                aiFaceInputRequest.setMember_id(aiRequestCommand.getMember_id());
                aiFaceInputRequest.setUnion_id(aiRequestCommand.getUnion_id());
                aiFaceInputRequest.setFeature_face(aiRequestCommand.getFeature_face());
                aiFaceInputRequest.setFeature_image(aiRequestCommand.getFeature_image());
                aiResponse = aiClient.execute(aiFaceInputRequest);
                if (AIConstant.CODE_SUCCESS.equals(aiResponse.getErrorCode())) {
                    AIFaceInputResponse aiFaceInputResponse = (AIFaceInputResponse) aiResponse.getData();
                    aiFaceInputResponse.setErrorCode(aiResponse.getErrorCode());
                    return aiFaceInputResponse;
                }else{
                    AIFaceInputResponse aiFaceInputResponse = new AIFaceInputResponse();
                    aiFaceInputResponse.setErrorCode(aiResponse.getErrorCode());
                    aiFaceInputResponse.setErrorMsg(AiExceptionCode.get(aiResponse.getErrorCode()).msg);
                    return aiFaceInputResponse;
                }
            case INPUTFACE_UPDATE_COLLECT:
                UpdateFaceInputRequest updateFaceInputRequest = new UpdateFaceInputRequest();
                updateFaceInputRequest.setUnion_id(aiRequestCommand.getUnion_id());
                updateFaceInputRequest.setMember_id(aiRequestCommand.getMember_id());
                updateFaceInputRequest.setFeature_face(aiRequestCommand.getFeature_face());
                updateFaceInputRequest.setFeature_image(aiRequestCommand.getFeature_image());
                updateFaceInputRequest.setFace_id(aiRequestCommand.getFace_id());
                aiResponse = aiClient.execute(updateFaceInputRequest);
                if (AIConstant.CODE_SUCCESS.equals(aiResponse.getErrorCode())) {
                    UpdateFaceInputResponse updateFaceInputResponse = (UpdateFaceInputResponse) aiResponse.getData();
                    updateFaceInputResponse.setErrorCode(aiResponse.getErrorCode());
                    return updateFaceInputResponse;
                }else{
                    UpdateFaceInputResponse updateFaceInputResponse = new UpdateFaceInputResponse();
                    updateFaceInputResponse.setErrorCode(aiResponse.getErrorCode());
                    updateFaceInputResponse.setErrorMsg(AiExceptionCode.get(aiResponse.getErrorCode()).msg);
                    return updateFaceInputResponse;
                }
            default:
        }

        return aiResponse;
    }

    public static void main(String[] args)throws  AiException{
        //人脸查询2.0
        AIRequestCommand aiRequestCommand = new AIRequestCommand();
        aiRequestCommand.setAiMethod(AIMethod.INPUTFACE_PRECOLLECT);
        aiRequestCommand.setMember_id("1");
        aiRequestCommand.setUnion_id("10815-8888");
        aiMethod(aiRequestCommand);

//        AIRequestCommand aiRequestCommand = new AIRequestCommand();
//        aiRequestCommand.setAiMethod(AIMethod.INPUTFACE_COLLECT);
//        aiRequestCommand.setUnion_id("10815-8888");
//        aiRequestCommand.setMember_id("6666");
//        aiRequestCommand.setFeature_face("1");
//        aiRequestCommand.setFeature_image("http://testmch.chuangjiangx.com/content/images/b1484d92.logo.png");
//        aiMethod(aiRequestCommand);

//        AIRequestCommand aiRequestCommand = new AIRequestCommand();
//        aiRequestCommand.setAiMethod(AIMethod.INPUTFACE_UPDATE_COLLECT);
//        aiRequestCommand.setUnion_id("10815-8888");
//        aiRequestCommand.setMember_id("6666");
//        aiRequestCommand.setFeature_face("1");
//        aiRequestCommand.setFeature_image("http://chuangjiangx.oss-cn-hangzhou.aliyuncs.com/1538040748395/%E8%8D%89%E8%8E%93%E5%A5%A5%E5%88%A9%E5%A5%A5.png");
//        aiMethod(aiRequestCommand);

    }

}
